package org.aphelie.torg.ruler.tests.internal.core.character;

import java.io.CharConversionException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.List;

import junit.framework.TestCase;

import org.aphelie.torg.ruler.core.character.ICharacter;
import org.aphelie.torg.ruler.core.character.ICharacterBase;
import org.aphelie.torg.ruler.core.character.ICharacterExtension;
import org.aphelie.torg.ruler.core.character.ICharacterLibrary;
import org.aphelie.torg.ruler.core.character.ICharacterModelBase;
import org.aphelie.torg.ruler.core.character.ICharacterObject;
import org.aphelie.torg.ruler.core.character.ISharedCharacterModel;
import org.aphelie.torg.ruler.internal.core.character.WorkspaceCharacterModel;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.ui.internal.dnd.InsertCaret;

public class WorkspaceCharacterModelTest extends TestCase {

	public final static String NAME = "WorkspaceCharacterModelTest";

	private static String PROJECT_NAME = "Primo";
	private static String CHARACTER_FILENAME = "Primus.tgk";

	private IProject fProject;
	private IFile fCharacterFile;

	public WorkspaceCharacterModelTest() {
		super(NAME);
		IWorkspace workspace = ResourcesPlugin.getWorkspace();
		fProject = workspace.getRoot().getProject(PROJECT_NAME);
		fCharacterFile = fProject.getFile(CHARACTER_FILENAME);
	}

	public void setupCharacterFile() {

		IWorkspaceRunnable operation = new IWorkspaceRunnable() {
			public void run(IProgressMonitor monitor) throws CoreException {
				getProject().create(monitor);
				getProject().open(monitor);
				getCharacterFile().create(null, IResource.NONE, monitor);

			}
		};

		try {
			getProject().getWorkspace().run(operation, new NullProgressMonitor());
            //write(getCharacterFile());
		} catch (CoreException e) {
			e.printStackTrace();
		}

		assertTrue(getCharacterFile().exists());
	}

	public void testWorkspaceCharacterModel() throws CoreException {

		setupCharacterFile();

		WorkspaceCharacterModel wsModel = new WorkspaceCharacterModel(
				getCharacterFile(), false);
		ICharacter character = wsModel.getCharacter();

		character.setId("trucIp");
		character.setProviderName("ProviderName");
		
		ICharacterExtension extension = wsModel.createExtension();
		extension.setName("TrucExtension");
		
		//List myList = Collections.EMPTY_LIST;
		//myList.add(extension);
		
		
		//character.add(extension);
		
		ICharacterLibrary library = wsModel.createLibrary();
		character.add(library);
		
		
		
		//System.err.println("character.getExtensions().length " + character.getExtensions().length);
		
		System.err.println(wsModel.getUnderlyingResource());
		System.err.println(wsModel.getContents());

		IWorkspaceRunnable operation = new IWorkspaceRunnable() {
			public void run(IProgressMonitor monitor) throws CoreException {
				File destFile = new File("/home/cdoens/tmp/Primus.tgk");
				try {
					copy(getCharacterFile(), destFile);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		};

		//operation.run(new NullProgressMonitor());
	}
	
	
	
	

	public IProject getProject() {
		return fProject;
	}

	public IFile getCharacterFile() {
		return fCharacterFile;
	}
	
	public static void write(IFile source) throws CoreException, IOException {
	    InputStream in = source.getContents();
	    OutputStream out = new FileOutputStream(new File(source.getLocation().toOSString()));
	    copy(in, out);
	}

	public static void copy(IFile source, File destination) throws IOException {
		copy(source.getLocation().toFile(), destination);
	}

	public static void copy(File source, File destination) throws IOException {

		InputStream in = new FileInputStream(source);
		OutputStream out = new FileOutputStream(destination);
		copy(in, out);
	}
	
    public static void copy(InputStream in, OutputStream out) throws IOException {
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }

}